package com.web.controller;

import com.web.entity.Message;
import com.web.entity.Staff;
import com.web.service.MessageService;
import com.web.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller("messageController")
public class MessageController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private StaffService staffService;
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Message> list = messageService.getAll();
        request.setAttribute("LIST",list);
        request.getRequestDispatcher("../message_list.jsp").forward(request,response);
    }
    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Staff> list = staffService.getAll();
        request.setAttribute("DLIST",list);
        request.getRequestDispatcher("../message_add.jsp").forward(request,response);
    }
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        Integer sid = Integer.parseInt(request.getParameter("sid"));
        Message message =new Message();
        message.setTitle(title);
        message.setContent(content);
        message.setSid(sid);

        messageService.add(message);

        response.sendRedirect("list.do");
    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Message message = messageService.get(id);
        request.setAttribute("OBJ",message);
        List<Staff> list = staffService.getAll();
        request.setAttribute("DLIST",list);
        request.getRequestDispatcher("../message_edit.jsp").forward(request,response);
    }
    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        Integer sid = Integer.parseInt(request.getParameter("sid"));
        Message message =messageService.get(id);
        message.setTitle(title);
        message.setContent(content);
        message.setSid(sid);
        messageService.edit(message);
        response.sendRedirect("list.do");
    }
    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        messageService.remove(id);
        response.sendRedirect("list.do");
    }

    public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Message message =messageService.get(id);
        request.setAttribute("OBJ",message);
        request.getRequestDispatcher("../message_detail.jsp").forward(request,response);
    }
}
